package notes.java.basic.ticket;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/** 模拟售票*/
public class TicketSeller2 {
	
	static Queue<Integer> tickets = new ConcurrentLinkedQueue<>();
	static int threadNum = 10;
	// 初始化1000张票
	static {
		for(int i=0; i<1000; i++) {
			tickets.add(i);
		}
	}
	
	public static void main(String[] args) {
		
		// 10个线程同时售票
		for(int i=0; i<threadNum; i++) {
			new Thread(()->{
				while(true) {
					Integer num = tickets.poll();
					if(num == null) break;
					System.out.println(Thread.currentThread().getName() + "售出"  + num + "剩余："+ tickets.size());
				}
			}, "线程名" + i).start();
		}
		
	}
}